package vashion.azeroth.api.controller.tran;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.common.common.check.FormatCheck;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.manager.personal.tran.PersonalDisburseManager;
import vashion.azeroth.core.manager.personal.tran.PersonalRechargeOrderManager;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.personal.tran.pojo.PersonalBankInfo;
import vashion.azeroth.core.personal.tran.pojo.PersonalDisburse;
import vashion.azeroth.core.personal.tran.pojo.PersonalRechargeOrder;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalMemberService;
import vashion.azeroth.core.service.tran.PersonalDisburseService;

@Controller
@RequestMapping("/tran")
public class PersonalDisburseController extends BaseController {

	@Autowired
	private PersonalDisburseService<PersonalDisburse> disburseService;
	@Autowired
	private PersonalDisburseManager disburseManager;
	@Autowired
	private PersonalMemberService<PersonalMember> memberService;
	@Autowired
	private PersonalRechargeOrderManager rechargeOrderManager;
	
	/**
	 * 创建消费订单接口
	 * @param memberId
	 * @param disburse
	 * @return
	 */
	@RequestMapping("createDisburse.htm")
	@ResponseBody
	public ApiResult<String> createDisburseNo(Long memberId, PersonalDisburse disburse) {
		
		//验证商品价格
		if(!FormatCheck.isNumeric(String.valueOf(disburse.getTotalPrice()))) {
			return ApiResult.newErrorResult(GenericErrorEnum.PARAMETER_ERROR.getErrorCode(), "付款总额" + GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
		}
		
		PersonalMember member = memberService.get(memberId);
		
		if(null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "用户" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		
		return disburseService.createDisburse(member, disburse);
	}
	
	@RequestMapping("modifyDisburseAddress.htm")
	@ResponseBody
	public ApiResult<String> modifyDisburseAddress(PersonalDisburse disburse) {
		
		if(null == disburse.getBuyerMemberId()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "买家memberId" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		if(null == disburse.getDisburseNo()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "消费订单号" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		return disburseService.modifyDisburseAddress(disburse);
	}
	
	@RequestMapping("uploadCertificatePic.htm")
	@ResponseBody
	public ApiResult<String> uploadCertificatePic(PersonalDisburse disburse) {
		
		if(null == disburse.getDisburseNo()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "消费订单号" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		return disburseService.uploadCertificatePic(disburse);
	}
	
	@RequestMapping("queryBankInfo.htm")
	@ResponseBody
	public ApiResult<PersonalBankInfo> queryBankInfo(Long memberId) {
		return disburseService.queryBankInfo(memberId);
	}
	
	@RequestMapping("orderPriceModify.htm")
    @ResponseBody
    public ApiResult<String> orderPriceModify(String disburseNo, Long orderPrice) {
    	PersonalDisburse disburse = disburseService.getByDisburseNo(disburseNo);
    	if(null == disburse) {
    		return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "订单号" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
    	}
    	
    	Long agio = disburse.getOrderTotalPrice() - orderPrice;
    	
    	disburse.setTotalPrice(disburse.getTotalPrice() - agio);
    	disburse.setOrderTotalPriceOld(disburse.getOrderTotalPrice());
    	disburse.setOrderTotalPrice(disburse.getOrderTotalPrice() - agio);
    	disburse.setTimeEdit(new Date());
    	
    	int count = disburseManager.update(disburse);
    	
    	PersonalRechargeOrder rechargeOrder = rechargeOrderManager.getRechargeOrderByDisburseNo(disburseNo);
    	if(null != rechargeOrder) {
    		rechargeOrder.setAmount(rechargeOrder.getAmount() - agio);
    		rechargeOrder.setRemark("修改价格更新");
    		rechargeOrderManager.update(rechargeOrder);
    	}
    		
    	if(count > 0) {
    		return ApiResult.newSuccessResult();
    	}
    	return ApiResult.newErrorResult(GenericErrorEnum.UPDATE_ERROR.getErrorCode(), GenericErrorEnum.UPDATE_ERROR.getErrorMessage());
    }

	
}
